<?php	
	// Interface function	
	function oh_module_user($tokens) {
		global $oh_object_user;
		if (count($tokens)>0) {
			return $_POST[$tokens[0]];
		}
		return $oh_object_user;	
	} 

	// Option file required
	require_once($oh['path']."/modules/user/class.pauth.php");
	
	// Main class of module
	class oh_class_user extends pauth {
		public $user_attual;
		public $try_login;
		public $try_register;
		public $datasource;
		function oh_class_user() {
			$this->try_login 	= false;
			$this->try_register = false;
			$this->datasource 	= oh_find_data_source(array(
				"mysql",
				"gladius"
			));
			$this->pauth(3600, oh("path.script")."/");			
		}
		// test
		function is_registered($username) {
			if (false) {			
			} else if ($this->user_source == "mysql") {			
				$pfx = oh("mysql")->get_prefix();				
				$sql = "SELECT id FROM {$pfx}object WHERE class=999001 AND uniqid='{$username}' LIMIT 1";
				$res = oh("mysql")->get_results($sql);
				if (count($res)>0) {
					return true;	
				} else {
					return false;	
				}
			} else if ($this->user_source == "database") {			
			} else if ($this->user_source == "ldap") {			
			} else if ($this->user_source == "xml") {			
			} else {
				$users = oh("frecord")->get($this->user_source);
				foreach($users as $line=>$user) {
					parse_str($user,$userdata);					
					if ($username == $userdata['username']) {
						$this->user_attual = $userdata;
						return true;
					}
				}
			}			
			return false;
		}
		function is_logged($area="default") {
			$is_logged = $this->testLogin($area) || !empty($this->user['login']);
			if (!$is_logged) {
				if (!$this->try_login) {
					if ($this->login_try()) {							
						$username = $_POST['username'];
						$password = md5($_POST['password']);
						$is_logged = oh("model")->hit_object("user&uniq=$username&pass=$password&mood=1");						
						if ($is_logged) {
							$this->login(array(
								"username" => $username
							));
						}
						return $is_logged;
					}
				}
			}
			return $is_logged;
		}
		// pseudo event
		function login_try() {
			$this->try_login = true;
			return oh("simpleform")->onsubmit("loginform");
		}
		function register_try() {
			return oh("simpleform")->onsubmit("registerform");
		}
		// forms	
		function login_form() {
			oh("simpleform")->open("loginform");
			oh("simpleform")->label('Username');
			oh("simpleform")->text('username');oh("br");
			oh("simpleform")->label('Password');
			oh("simpleform")->password('password');
			oh("simpleform")->submit('Login');
			oh("simpleform")->close();
		}
		function register_form() {
			oh("simpleform")->open("registerform");
			oh("simpleform")->label('Username');
			oh("simpleform")->text('username');oh("br");
			oh("simpleform")->label('Email');
			oh("simpleform")->text('email');oh("br");
			oh("simpleform")->label('Password');
			oh("simpleform")->password('password');oh("br");
			oh("simpleform")->label('Repeat Password');
			oh("simpleform")->password('repassword');
			oh("simpleform")->submit('Register');
			oh("simpleform")->close();		
		}
		// action
		function login($data) {
			$data['area'] = (!isset($data['area'])) ? "default" : strtolower(trim($data['area']));
			$this->pauth_login($data['username'], $data['area']);
		}
		function logout($data) {
			$data['area'] = (!isset($data['area'])) ? "default" : strtolower(trim($data['area']));
			$this->pauth_logout($data['area']);
		}
		function register($data) {
			if (false) {			
			} else if ($this->user_source == "mysql") {			
				$pfx = oh("mysql")->get_prefix();
				$sql = "INSERT INTO {$pfx}object (class,uniqid,name,password,email,created) VALUES (999001,'{$data['username']}','{$data['name']}','{$data['password']}','{$data['email']}',NOW())"; //'
				oh("mysql")->query($sql);
			} else if ($this->user_source == "database") {			
			} else if ($this->user_source == "ldap") {			
			} else if ($this->user_source == "xml") {			
			} else {
				$data['password'] = md5($password);
				$userdata = array();
				foreach($data as $key=>$value) {
					$userdata[] = $key.'='.$value;
				}
				$userdata = implode("&",$userdata);
				oh("frecord")->prepend($this->user_source,$userdata);
			}
		}
		// retrive data
		function get_all() {
			$pfx = oh("mysql")->get_prefix();
			$sql = "SELECT * FROM {$pfx}object WHERE class=999001";
			return oh("mysql")->get_results($sql);
		}
		function add($username,$password,$groups) {
			$password = $this->hash_password($password);
			oh("model")->new_object("user&uniq=$username&pass=$password&mood=1");
		}
		function hash_password($password) {
			return md5($password);
		}
	}
	
	// Main object of module
	global $oh_object_user;
	$oh_object_user = new oh_class_user();
?>
